package com.push.alipush;

import android.content.Context;
import android.content.Intent;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;

import java.util.HashMap;
import java.util.Map;

/**
 * 接收推送消息
 * Created by liaoyacheng on 2021/8/4.
 */
public class AliPushMessageReceiver extends MessageReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if("com.push.CLICK_ACTION".equals(action)) {
            String params = intent.getStringExtra("params");
            AlipushFlutterPlugin.instance.runMainThread(params, null, "onNotificationClicked");
        } else {
            super.onReceive(context, intent);
        }
    }

    /**
     * 用于接收服务端推送的通知，通知会弹窗，通知接收通知以及点击通知等都有回调。
     * 客户端接收到通知后，回调该方法。
     *
     * @param title    通知标题
     * @param summary  通知内容
     * @param extraMap 通知额外参数，包括部分系统自带参数：
     *                 _ALIYUN_NOTIFICATION_ID_(V2.3.5及以上):创建通知对应id
     *                 _ALIYUN_NOTIFICATION_PRIORITY_(V2.3.5及以上):创建通知对应id。默认不带，需要通过OpenApi设置
     */
    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        log("Receive notification, title: " + title + ", summary: " + summary
                + ", extraMap: " + extraMap);
        HashMap<String, Object> map = new HashMap<>();
        map.put("title", title);
        map.put("content", summary);
        map.put("extra", extraMap);
//        AlipushFlutterPlugin.instance.runMainThread(map, null, "onNotification");
        AlipushFlutterPlugin.instance.runMainThread(map, null, "onReceiveNotification");
    }

    /**
     * 用于接收服务端推送的消息，消息不会弹窗，而是回调该方法。
     *
     * @param context      android组件上下文
     * @param cPushMessage 推送的消息
     */
    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        log("onMessage, messageId: " + cPushMessage.getMessageId() + ", title: "
                + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
        HashMap<String, Object> map = new HashMap<>();
        map.put("appId", cPushMessage.getAppId());
        map.put("messageId", cPushMessage.getMessageId());
        map.put("traceInfo", cPushMessage.getTraceInfo());
        //主要是content，其他没多大用
        map.put("title", cPushMessage.getTitle());
        map.put("content", cPushMessage.getContent());
//        AlipushFlutterPlugin.instance.runMainThread(map, null, "onMessage");
        AlipushFlutterPlugin.instance.runMainThread(map, null, "onReceiveNotification");
    }

    /**
     * 打开通知时会回调该方法，通知打开上报由SDK自动完成。
     *
     * @param title    通知标题
     * @param summary  通知内容
     * @param extraMap 通知额外参数，包括部分系统自带参数：
     *                 _ALIYUN_NOTIFICATION_ID_(V2.3.5及以上):创建通知对应id
     *                 _ALIYUN_NOTIFICATION_PRIORITY_(V2.3.5及以上):创建通知对应id。默认不带，需要通过OpenApi设置
     */
    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        log("onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
        HashMap<String, Object> map = new HashMap<>();
        map.put("title", title);
        map.put("content", summary);
        map.put("extra", extraMap);
        AlipushFlutterPlugin.instance.runMainThread(map, null, "onOpenNotification");
    }

    /**
     * 无跳转逻辑通知打开回调
     * 打开无跳转逻辑(open=4)通知时回调该方法(v2.3.2及以上版本支持)，通知打开上报由SDK自动完成
     */
    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        log("onNotificationClickedWithNoAction, title: " + title + ", summary: "
                + summary + ", extraMap:" + extraMap);
        HashMap<String, Object> map = new HashMap<>();
        map.put("title", title);
        map.put("content", summary);
        map.put("extra", extraMap);
        AlipushFlutterPlugin.instance.runMainThread(map, null, "onNotificationClickedWithNoAction");
    }

    /**
     * 当用户创建自定义通知样式，并且设置推送应用内,到达不创建通知弹窗时调用该回调，且此时不调用onNotification回调(v2.3.3及以上版本支持)参数。
     *
     * @param extraMap     通知额外参数
     * @param openType     原本通知打开方式，1：打开APP；2：打开activity；3：打开URL；4：无跳转逻辑
     * @param openActivity 所要打开的activity的名称，仅当服务端参数openType=2时有效，其余情况为null
     * @param openUrl      所要打开的URL，仅当服务端参数openType=3时有效，其余情况为null
     */
    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap
            , int openType, String openActivity, String openUrl) {
        log("onNotificationReceivedInApp, title: " + title + ", summary: " + summary
                + ", extraMap:" + extraMap + ", openType:" + openType
                + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
        HashMap<String, Object> map = new HashMap<>();
        map.put("title", title);
        map.put("content", summary);
        map.put("extra", extraMap);
        map.put("openType", openType);
        map.put("openActivity", openActivity);
        map.put("openUrl", openUrl);
        AlipushFlutterPlugin.instance.runMainThread(map, null, "onNotificationReceivedInApp");
    }

    /**
     * 删除通知时回调该方法，通知删除上报由SDK自动完成。
     */
    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        log("onNotificationRemoved");
        HashMap<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        AlipushFlutterPlugin.instance.runMainThread(map, null, "onNotificationRemoved");
    }

    private void log(String message){
        AlipushFlutterPlugin.instance.log("AliMessageReceiver", message);
    }
}
